#include <bits/stdc++.h>
using namespace std;

long long start[1000011];
long long cnt[200011];
long long mx[200011];
long long cntmx[200011];
long long a[200011];
bool color[200011];
//long long cnt2[200011];
long long lft[200011];
long long rig[200011];
long long n;

void dfs(long long l , long long r , bool col){
    //cout << l << " " << r << "\n";
    if(l < 2) return ;
    if(r > n + 1) return ;
    //cout << l << " " << r << "\n";
    bool flag = 1;
    for(long long i = l;i < r;i++){
        if(a[i] != a[i + 1]){
            flag = 0;
            break;
        }
        cnt[i] = 0;
    }
    cnt[r] = 0;
    if(flag){
        for(long long i = l;i <= r;i++){
            color[i] = col;
        }
        return ;
    }
    if(l >= r) return ;
    if(l == r){
        color[l] = col;
        return ;
    }
    //long long mx = -1;
    long long l1 = l;
    long long r1 = r;
    for(long long i = l;i <= r;i++){
        if(a[i] == a[l - 2]){
            bool x = color[l - 2];
            bool x2 = x ^ 1;
            for(long long j = l;j < i;j++){
                color[j] = x2;
            }
            color[i] = x;
            l1 = i + 1;

        }
        if(a[i] == a[r + 2]){
            bool x = color[r + 2];
            bool x2 = x ^ 1;
            for(long long j = i + 1;j <= r;j++){
                color[j] = x2;
            }
            color[i] = x;
            r1 = i - 1;
        }
    }
    //cout << l1 << " " << r1 << "!\n";
    if(l1 >= r1) return ;
    long long mx = -1;
    long long mx_num = -1;
    //cout << l1 << " " << r1 << "-=erwq\n";
    for(long long i = l1;i < r1;i++){
        if(a[i] == a[i + 1]){
            i++;
            continue;
        }
        cnt[a[i]]++;
        if(cnt[a[i]] == 1){
            lft[a[i]] = i;
        }
        rig[a[i]] = i;
        if(cnt[a[i]] >= 2){
            if((cnt[a[i]] - 1) * a[i] > mx){
                mx = (cnt[a[i]] - 1) * a[i];
                mx_num = a[i];
            }
        }
    }
    if(a[r1] != a[r1 - 1]){
        cnt[a[r1]]++;
        if(cnt[a[r1]] == 1){
            lft[a[r1]] = r1;
        }
        rig[a[r1]] = r1;
        if(cnt[a[r1]] >= 2){
            if((cnt[a[r1]] - 1) * a[r1] > mx){
                mx = (cnt[a[r1]] - 1) * a[r1];
                mx_num = a[r1];
            }
        }
    }
    if(mx_num == -1){
        for(long long i = l;i <= r;i++){
            a[i] = col;
        }
        return ;
    }
    //cout << mx_num << "\n";
    bool col2 = col ^ 1;
    for(long long i = lft[mx_num];i <= rig[mx_num];i++){
        if(a[i] == mx_num){
            color[i] = col;
        }
        else{
            color[i] = col2;
        }
    }
    //for(long long i = 2;i <= n + 1;i++){
        //cout << color[i] << " ";
    //}
    dfs(l1 , lft[mx_num] - 1 , col2);
    dfs(rig[mx_num] + 1 , r1 , col2);
    return ;
}

void solve(){
    memset(color , 0 , sizeof(color));
    memset(cnt , 0 , sizeof(cnt));
    //long long n;
    cin >> n;
    for(long long i = 2;i <= n + 1;i++){
        cin >> a[i];
    }
    //cout<<"!@$#$^%$^%$%";
    //for(long long i = 798;i <= 811;i++) cout << a[i];
    //cout<<"\n";
    dfs(2 , n + 1 , 0);
    //    if(!cnt[x]){
    //        start1[x] = i;
    //    }
    //    cnt[x]++;
    //    if(x * (cnt[x] - 1) > mx[i - 1] * (cntmx[i - 1] - 1)){
    //        mx[i] = x;
    //        mxcnt[i] = cnt[x];
    //    }
    //    else{
    //        mx[i] = mx[i - 1];
    //        mxcnt[i] = mxcnt[i - 1];
    //    }
    //}
    //if(mxcnt[n] <= 1){
    //    cout << "0\n";
    //    return 0;
    //}
    //long long now = n;
    //while(start[x] == 1 || mxcnt[now] <= 1){
    //    long long st = start[mx[now]];
    //    for(long long i = st;i <= now;i++){
    //
    //    }
    //}
    long long alst = -1;
    long long blst = -1;
    long long ans = 0;
    for(long long i = 2;i <= n + 1;i++){
        if(color[i]){
            if(a[i] == blst){
                ans += blst;
            }
            blst = a[i];
        }
        else{
            if(a[i] == alst){
                ans += alst;
            }
            alst = a[i];
        }
        //cout << color[i] << " ";
    }
    cout << ans << "\n";
    return ;
}

int main(){
    freopen("color.in" , "r" , stdin);
    freopen("color.out" , "w" , stdout);
    long long t;
    cin >> t;
    while(t--){
        solve();
    }
    //cout << "\n";
    return 0;
}
//5 4 3 3 4 5
//B R R R R B
//6 5 4 3 2 1 1 2 3 4 5 6
//B R B R B R R B R B R B 1 + 2 = 3
//B R R R R R R R R R R B 1 + 6 = 7
//6 5 6 4 5 4
//R B R R B R 6 + 5 + 4 = 15
//6 5 4 6 5 4 3 4 5 3
//R B B R R B R B B R 6 + 4 + 4 + 3 = 17
//R B B R R B B B R B 6 + 4 = 10
//R B R R B R B R R B 5 + 4 + 3 = 12
//R B R B B R R R B R 4 + 5 = 9
//R B B R R B R B B R 6 + 4 + 4 + 3 = 17
//3 5 2 5 1 2 1 4
//R R B R R B R R 5 + 2 + 1 = 8
//5 3 7 2 4 13 11 6 5 5 3 5 12 8 13
//R R R R R R  B  B B B B B B  B R 13 + 5 = 18
//1 12 11 11 7 11 15 6 4 6 3 15 7 5 2
//B B  R  R  B R  R  B B B B R  B B B 11 + 11 + 15
//4 6 3 3 6 4
//R B R R B R
//4 6 3 3 6 3 4 6 4
//R B R R B R R B R
//2 1 9 9 5 1 2
//R R B B R R R
//R B B B B B R
//DFS(L,R);
//DFS(L,l);
//DFS(r,R);
//IN DFS(L,R)
//WE FIND MAX(L TO R)
//IF A[X] == A[X+1] IGNORE IT
//7 4 7 5 4
//R B R B B
//5 4 7 5 4 7 5 4
//B R R B B R R B
//9 8 6 9 3 7 6 5 4 7 5 4
//R B B R R R B B B R B B
//ADD:IF WE FIND L TO R EQUAL L - 1 OR EQUAL R + 1
//ADD IT
//9 8 9 7 9
//R B R B R
//9 5 7 9 6 7 8 6 7 8
//R B B R B R R B B R
//B R R B B R R B B R
//ADD:PUT COLOR WILL NOT SAME
//JUST LIKE 1010101 OR 101010
//15
//1 12 11 11 7 11 15 6 4 6 3 15 7 5 2
